/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaChat.java
 *
 * Created on 02/12/2011, 15:45:29
 */
package chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

import javax.swing.JFrame;

/**
 * 
 * @author lucas
 */
public class TelaChat extends JFrame implements Runnable {

	public MulticastSocket s;

	private InetAddress address;
	private int porta;
	
	private String login;
	private javax.swing.JButton jButton1;
	private javax.swing.JScrollPane jScrollPane1;
	static javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1;

	/**
	 * Creates new form TelaChat
	 * 
	 * @throws IOException
	 */
	public TelaChat(Sala sala, final String login) {
		initComponents();
		this.login = login;
		try {
			this.address = InetAddress.getByName(sala.getEndereco());
			System.out.println(sala.getPorta() +":"+sala.getEndereco());
			this.porta = sala.getPorta();
			s = new MulticastSocket(this.porta);
			s.joinGroup(this.address);
		} catch (IOException e) {
			e.printStackTrace();
		}
		jTextArea1.setEditable(false);
		this.setVisible(true);
		this.setTitle(sala.getNome()); 
	}

	public TelaChat() {
		this.initComponents();
	}

	private void initComponents() {
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);
		
		jButton1.setText("Enviar");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton1MouseClicked(evt);
			}
		});
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																376,
																Short.MAX_VALUE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jTextField1,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				310,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				jButton1)))
										.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
						212, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18,
						18, 18).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										45,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jButton1)).addContainerGap(13,
						Short.MAX_VALUE)));

		pack();
	}
	
	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
		String mensagem = String.format("%s diz: %s", login, jTextField1.getText());
		this.jTextField1.setText("");
		DatagramPacket pacote = new DatagramPacket(mensagem.getBytes(), mensagem.getBytes().length,
				address, porta);
		try {
			s.send(pacote);
		} catch (IOException ex) {
			javax.swing.JOptionPane.showMessageDialog(null, "Problema");
		}
	}

	public void setDefaultCloseOperation(int operation){
		this.hide();
	}

	@Override
	public void run() {
		while (true) {
			try {
				byte[] resp = new byte[100];
				DatagramPacket rec = new DatagramPacket(resp, resp.length);
				s.receive(rec);
				jTextArea1.setText(jTextArea1.getText()+ "\n" + new String(rec.getData()));
			} catch (IOException ex) {
				System.err.println("Erro");
			}
		}
	}
}